/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.meraklisina.latexeditor.mdi.DialogActions;



import java.io.File;
import java.io.FileWriter;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;

import com.meraklisina.latexeditor.mdi.Common.FileOperations;
import com.meraklisina.latexeditor.mdi.Common.Tools;
import com.meraklisina.latexeditor.mdi.FileFilters.FileFilterTEX;
import com.meraklisina.latexeditor.mdi.TextEditor.TextEditors;

/**
 *
 * @author Eray
 */
public class ActionSave
{
    TextEditors ClsTextEditor = new TextEditors();

//    public void ShowSaveDialog(JFrame Form, JTabbedPane TabEditors, boolean IsSaveAs)
//    {
//        try
//        {
//             //if(FormMain.WorkingFilePath.isEmpty() || IsSaveAs || !FormMain.IsTextChanged)
//             if(ClsTextEditor.GetRealPath(TabEditors.getSelectedIndex()).isEmpty() || IsSaveAs || !ClsTextEditor.GetIsTextChanged(TabEditors.getSelectedIndex()))
//             {
//                 SaveDialog(Form, TabEditors, "Save As...", IsSaveAs);
//             }
//             else if(!ClsTextEditor.GetRealPath(TabEditors.getSelectedIndex()).isEmpty())
//             {
//                 Save(Form, TabEditors, ClsTextEditor.GetRealPath(TabEditors.getSelectedIndex()), false);
//             }
//         }
//         catch(Exception ex)
//         {
//            JOptionPane.showMessageDialog(null,ex.getMessage(),"File error",JOptionPane.ERROR_MESSAGE);
//         }
//
//    }
    /**
     * This is the Save operation that actually writes a into physical file.
     * <p>
     * <b>IsScrapFile</b> parameter actually indicates the "Dirty Flag". So,
     * this parameter will generally be used with the <b>false</b> value.
     * When you save a file, the flag will be changed
     * into "false" with this parameter. But, for the later versions of the application,
     * <b>AutoSave</b> feature will operate on this functions and the saved file
     * will be a <b>Scrap</b> file. Only then, the value will be <b>true</b>.
     * </p>
     *
     * @param Form
     * @param TabEditors
     * @param FilePath
     * @param IsScrap
     */
    public void Save(JTabbedPane TabEditors, String FilePath, boolean IsScrapFile)
    {
       Save(TabEditors, TabEditors.getSelectedIndex(), FilePath, IsScrapFile);
    }
    /**
     * This is the Save operation that actually writes a into physical file.
     * <p>
     * <b>IsScrapFile</b> parameter actually indicates the "Dirty Flag". So,
     * this parameter will generally be used with the <b>false</b> value.
     * When you save a file, the flag will be changed
     * into "false" with this parameter. But, for the later versions of the application,
     * <b>AutoSave</b> feature will operate on this functions and the saved file
     * will be a <b>Scrap</b> file. Only then, the value will be <b>true</b>.
     * </p>
     *
     * @param Form
     * @param TabEditors
     * @param index
     * @param FilePath
     * @param IsScrap
     */
    public void Save(JTabbedPane TabEditors, int index, String FilePath, boolean IsScrapFile)
    {
        try
        {
            FileWriter writeFile = new FileWriter(FilePath);
            //writeFile.write(Form.txt_TextField.getText());
            ////writeFile.write(ClsTextEditor.GetText(Form, TabEditors.getSelectedIndex()));
            writeFile.write(ClsTextEditor.GetTextAt(TabEditors, index));
            writeFile.close();

            //ClsTextEditor.SetTextChange(Form, TabEditors, IsScrapFile);
            ClsTextEditor.SetTextChange(TabEditors, index, IsScrapFile);
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null,ex.getMessage(),"File error",JOptionPane.ERROR_MESSAGE);
        }
    }
    /**
     * Shows a SaveDialog.
     *
     * <p>
     * This functions can also be used as "Save As" dialog with the <b>IsSaveAs</b>
     * dialog.
     * </p>
     *
     * @param TabEditors
     * @param DialogTitle
     * @param IsSaveAs
     */
    public void SaveDialog(JTabbedPane TabEditors, String DialogTitle, boolean IsSaveAs)
    {

         //Lastsaved Toolsda static olarak startup patthden gelcek
        try
        {
             JFileChooser fileChooser = new JFileChooser ();

             String LastOpenFilePath = ClsTextEditor.GetRealPath(TabEditors.getSelectedIndex());
             if(LastOpenFilePath.isEmpty())
                LastOpenFilePath = Tools.getLastSavedDirectory();

             File lastOpen = new File(LastOpenFilePath);

             fileChooser.setCurrentDirectory(lastOpen);
             fileChooser.setDialogTitle(DialogTitle);
             fileChooser.setFileFilter(new FileFilterTEX());
             // Open chooser dialog

             int result = fileChooser.showSaveDialog (null);

             if (result == JFileChooser.APPROVE_OPTION)
             {

                     File fFile = new File(fileChooser.getSelectedFile().toString());
                     fFile = fileChooser.getSelectedFile();


                     FileOperations ClsFile = new FileOperations();
                     fFile = ClsFile.HandleExtension(fFile, "tex");
                     if(!IsSaveAs)
                        ClsTextEditor.SetRealPath(TabEditors.getSelectedIndex(), fFile.getPath());

                     if (fFile.exists())
                     {
                          if(JOptionPane.showConfirmDialog (null,
                            "Overwrite existing file?","Confirm Overwrite",
                             JOptionPane.YES_NO_OPTION,
                             JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION)
                          {
////                              FileWriter writeFile = new FileWriter(fFile.getPath());
////                              // JOptionPane.showMessageDialog(null, Form.txt_TextField.getText());
////                              //writeFile.write(Form.txt_TextField.getText());
////                              writeFile.write(ClsTextEditor.GetText(TabEditors, TabEditors.getSelectedIndex()));
////                              writeFile.close();

                              Save(TabEditors, fFile.getPath(), false);
                          }
                      }
                      else
                      {
////                         FileWriter writeFile = new FileWriter(fFile.getPath());
////                         //writeFile.write(Form.txt_TextField.getText());
////                         writeFile.write(ClsTextEditor.GetText(TabEditors , TabEditors.getSelectedIndex()));
////                         writeFile.close();

                         Save(TabEditors, fFile.getPath(), false);
                      }

                     if(IsSaveAs)
                         ClsTextEditor.AddTab(TabEditors, false, "", fFile.getPath(), fFile); //if Save as, add a new tab and then display there.
                     else
                          ClsTextEditor.SetTextChange( TabEditors, false);

                     ////Form.setTitle(FormMain.AppTitle + " - " + FormMain.WorkingFilePath);
                     TabEditors.setTitleAt(TabEditors.getSelectedIndex(), ClsTextEditor.GetRealPathTitle(TabEditors.getSelectedIndex()));
             }
         }
         catch(Exception ex)
         {
             JOptionPane.showMessageDialog(null,ex.getMessage(),"File error",JOptionPane.ERROR_MESSAGE);
         }
    }
}
